﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Rhino.Mocks;
using SimpleServiceBus.Bus;
using SimpleServiceBus.Bus.Pipeline;
using SimpleServiceBus.Bus.Pipeline.PipelineComponents.MessageLogging;
using SimpleServiceBus.Endpoint;
using SimpleServiceBus.Endpoint.Metadata;

namespace SimpleServiceBus.Tests.Bus.Pipeline.PipelineComponents.MessageLogging
{
    [TestFixture]
    public class MessageLoggingComponentTestFixture
    {
        [Test]
        public void ComponentShouldFetchLoggerAndLogMessage()
        {
            //Given
            var pipeline = MockRepository.GenerateStub<IMessagePipeline>();
            pipeline.Stub(p => p.PipelineDirection).Return(PipelineDirection.Receive);
            
            var ep = MockRepository.GenerateStub<IMessagingEndpoint>();
            var epd = MockRepository.GenerateStub<IEndpointDescription>();
            epd.Stub(e => e.IncomingMessageLogLevel).Return(MessageLoggingLevel.BodyOnly);

            var ctx = MockRepository.GenerateMock<IMessageContext>();
            ctx.Stub(c => c.MessagePipeline).Return(pipeline);
            ctx.Stub(c => c.Endpoint).Return(ep);
            ctx.Stub(c => c.EndpointMetadata).Return(epd);
            pipeline.Stub(p => p.CurrentContext).Return(ctx);

            var logger = MockRepository.GenerateMock<IMessageLogger>();
            logger.Stub(l => l.LogMessage(ctx, MessageLoggingLevel.BodyOnly));

            ctx.Stub(c => c.GetLocalService<IMessageLogger>()).Return(logger);

            //When
            var component = new MessageLoggingComponent {Pipeline = pipeline};
            component.ProcessMessage(new MessageEnvelope());

            //Then
            logger.AssertWasCalled(l=>l.LogMessage(ctx,MessageLoggingLevel.BodyOnly));


        }
    }
}
